package main

import (
	"fmt"
	"qiutan/scraper"
	"time"
)

func main() {
	url := "https://vip.titan007.com/AsianOdds_n.aspx?id=2664060"
  match, realtimes := scraper.Scrape(url)

	targetTimeStr := match.Time

  fmt.Println(targetTimeStr)
  fmt.Println(realtimes[0].Home)

	// targetTime, err := parseTimeStr(targetTimeStr)
	//
	// if err != nil {
	// 	fmt.Println("Error parsing time string:", err)
	// 	return
	// }
	//
	// nextExecution := targetTime.Add(-10*time.Minute - 10*time.Second)
	//
	// fmt.Println("Target time:", targetTime.Format("2006-01-02 15:04:05"))
	// fmt.Println("First execution time:", nextExecution.Format("2006-01-02 15:04:05"))
	//
	// time.Sleep(time.Until(nextExecution))
	//
	// for nextExecution.Before(targetTime) {
	// 	waitDuration := time.Until(nextExecution)
	// 	if waitDuration > 0 {
	// 		time.Sleep(waitDuration)
	// 	}
	//
	// 	executeTask()
	//
	// 	nextExecution = nextExecution.Add(time.Minute)
	// }
	//
	// fmt.Println("End at:", time.Now().Format("2006-01-02 15:04:05"))
}

// Parse "timeStr" to +0800.
func parseTimeStr(timeStr string) (time.Time, error) {
	loc, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		fmt.Println("Failed to load location:", err)
		return time.Time{}, err
	}

	timeStr = timeStr[:len(timeStr)-10]

	t, err := time.ParseInLocation("2006-01-02 15:04", timeStr, loc)
	if err != nil {
		return time.Time{}, err
	}

	return t, nil
}

func executeTask() {
	fmt.Println("execute:", time.Now().Format("2006-01-02 15:04:05 -0700"))
}
